package com.sojson.util.threadLocal;

import javax.servlet.http.HttpServletRequest;

import com.sojson.util.StringUtil;
import com.sojson.util.id.GUIDUtil;
import com.sojson.util.servlet.ServletUtil;

/**
 * 自定义线程变量Bean
 * 
 * @author liu
 * @date 2021-08-12
 */
public class CustomThreadLocal<T> {

    /** 唯一标识 */
    private String id = GUIDUtil.generateGuid();

    /** 获取 唯一标识 */
    public String getId() {
        return id;
    }

    /** 设置 唯一标识 */
    public void setId(String id) {
        this.id = id;
    }

    /** 获取值 */
    @SuppressWarnings("unchecked")
    public T get() {
        return (T)ServletUtil.getValRequestNo(id);
    }

    /** 设置值 */
    public boolean set(T val) {
        return ServletUtil.setValRequestNo(id, val);
    }

    /** 删除值 */
    public void remove() {
        HttpServletRequest request = ServletUtil.getHttpServletRequest();
        if (StringUtil.isBlankObject(request)) {
            return;
        }
        ServletUtil.removeValRequest(id);
    }

}